﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using Microsoft.Phone.Controls;

namespace CannedHeater
{
    /*
     * Thanks to Claus Konrad: http://blog.clauskonrad.net/2010/09/wp7-navigationservice-support-when.html
     * Only minor changes (Uri's, ViewTypes), added comments
     */

    /// <summary>
    /// The different type of views
    /// </summary>
    public enum ViewType{ Home, Statistics, Settings }

    /// <summary>
    /// Controls navigation in the application.
    /// Necessary since only Pages can normally do navigation, Views cannot.
    /// </summary>
    public class ApplicationController
    {
        /// <summary>
        /// Storing single instance (singleton pattern)
        /// </summary>
        private static ApplicationController m_instance;

        /// <summary>
        /// Views associated with their Uri's
        /// </summary>
        private Dictionary<ViewType, Uri> m_Views;

        private ApplicationController()
        {
            m_Views = new Dictionary<ViewType, Uri>();

            // Register views with controller
            Register(ViewType.Home, new Uri("/", UriKind.Relative));
            Register(ViewType.Statistics, new Uri("/", UriKind.Relative));
            Register(ViewType.Settings, new Uri("/SettingsPage.xaml", UriKind.Relative));
        }

        /// <summary>
        /// Single instance of <see cref="ApplicationController"/>
        /// </summary>
        public static ApplicationController Default
        {
            get
            {
                if (m_instance == null)
                    m_instance = new ApplicationController();

                return m_instance;
            }
        }

        /// <summary>
        /// Register a <see cref="ViewType"/> with its Uri
        /// </summary>
        /// <param name="type">ViewType</param>
        /// <param name="address">Associated Uri (xaml path)</param>
        private void Register(ViewType type, Uri address)
        {
            if (m_Views.ContainsKey(type)) //update
            {
                m_Views[type] = address;
                return;
            }

            m_Views.Add(type, address); //add
        }

        /// <summary>
        /// Remove registered ViewType
        /// </summary>
        /// <param name="type">ViewType</param>
        private void UnRegister(ViewType type)
        {
            if (m_Views.ContainsKey(type))
                m_Views.Remove(type);
        }

        /// <summary>
        /// Navigate to specified View
        /// </summary>
        /// <param name="type">ViewType representing the View to navigate to</param>
        public void NavigateTo(ViewType type)
        {
            if (!m_Views.ContainsKey(type))
                return;

            Uri address = m_Views[type];

            //magic code here!
            PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
            Debug.Assert(root != null, "Root is null");
            root.Navigate(address);
        }
    }

}
